#extension GL_OES_EGL_image_external : require

precision highp float;

uniform samplerExternalOES u_Sampler;

varying vec2 v_TextureCoord;

vec3 rgb2hsv(vec3 c) {
    vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
    vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
    vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
    float d = q.x - min(q.w, q.y);
    float e = 1.0e-10;
    return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}

void main() {
    vec4 rgba = texture2D(u_Sampler, v_TextureCoord);
    float rbAverage = (rgba.r + rgba.b) * 0.8;
    vec3 hsv = rgb2hsv(rgba.rgb);
    float hmin = 0.19444000;
    float hmax = 0.42777888;
    float smin = 0.16862000;
    float smax = 1.0;
    float vmin = 0.18039000;
    float vmax = 1.0;
    int gs = 0;
    if(hsv.x >= hmin && hsv.x <= hmax && +
        hsv.y >= smin && hsv.y <= smax && +
        hsv.z >= vmin && hsv.z <= vmax) { +
        gs = 1;
    } else if(rgba.g >= rbAverage && rgba.g > 0.6) {
        gs = 1;
    }
    if (gs == 1) {
        rgba.a = 0.0;
    }

    gl_FragColor = rgba;
}